/**
 * 
 */
package com.vision.sales.cm.db.data;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import net.sf.gilead.pojo.gwt.LightEntity;

import com.vision.core.cm.db.data.Company;
import com.vision.sales.cm.data.TransactionType;

/**
 * 
 * @author Mark
 *
 */
@Entity
@Table(name = TransactionEntry.TABLE)
public class TransactionEntry extends LightEntity implements Serializable, Comparable<TransactionEntry> {

	private static final long serialVersionUID = -6841930334187286764L;
	
	public static final String TABLE = "transaction_entry";
	public static final String COL_COMPANY_ID = "company_id";
	public static final String COL_DATE = "date";
	public static final String COL_TYPE = "type";
	public static final String COL_TRANSACTION_ID = "transaction_id";
	public static final String COL_TRANSACTION_REF_NO = "transaction_ref_no";
	public static final String COL_CUSTOMER_ID = "customer_id";
	public static final String COL_SALESMAN_ID = "salesman_id";
	public static final String COL_PRODUCT_ID = "product_id";
	public static final String COL_DEBIT = "debit";
	public static final String COL_CREDIT = "credit";
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@Id
	@ManyToOne
	@JoinColumn(name = COL_COMPANY_ID)
	private Company company;

	@Id
	@Column(name = COL_DATE)
	@Temporal(TemporalType.DATE)
	private Date date;
	
	@Id
	@Column(name = COL_TYPE)
	@Enumerated(EnumType.STRING)
	private TransactionType type;
	
	/**
	 * The id of the transaction associated with this TransactionEntry.
	 * Transactions can be a Sales Invoice, Debit Memo, Credit Memo, or Collection Item.
	 */
	@Id
	@Column(name = COL_TRANSACTION_ID)
	private Long transactionId;
	
	@Column(name = COL_TRANSACTION_REF_NO, nullable = false)
	private String transactionRefNo;
	
	@ManyToOne
	@JoinColumn(name = COL_CUSTOMER_ID, nullable = false)
	private Customer customer;
	
	@ManyToOne
	@JoinColumn(name = COL_SALESMAN_ID, nullable = false)
	private Salesman salesman;
	
	@ManyToOne
	@JoinColumn(name = COL_PRODUCT_ID, nullable = false)
	private Product product;
	
	@Column(name = COL_DEBIT, nullable = false)
	private double debit;
	
	@Column(name = COL_CREDIT, nullable = false)
	private double credit;
	
	@Transient
	private double balance;
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	public TransactionEntry() {
		
	}
	
	@Override
	public int compareTo(TransactionEntry trans) {
		int diff = date.compareTo(trans.getDate());
		if (diff != 0) return diff;
		
		diff = transactionId.compareTo(trans.getTransactionId());
		if (diff != 0) return diff;
		
		diff = type.compareTo(trans.getType());
		if (diff != 0) return diff;
		
		return diff;
	}
	
	@Override
	public boolean equals(Object arg0) {
		if (arg0 instanceof TransactionEntry) {
			TransactionEntry t = (TransactionEntry) arg0;
			return company.equals(t.company)
					&& date.equals(t.date)
					&& transactionId.equals(t.transactionId)
					&& type.equals(t.type);
		}
		return super.equals(arg0);
	}
	
	// TODO Implement hashcode
	
	@Override
	public String toString() {
		return date + " " + customer.getId() + " " + transactionRefNo + " " + type + " " + debit + ", " + credit;
	}
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public TransactionType getType() {
		return type;
	}

	public void setType(TransactionType type) {
		this.type = type;
	}

	public Long getTransactionId() {
		return transactionId;
	}

	public void setTransactionId(Long transactionId) {
		this.transactionId = transactionId;
	}

	public String getTransactionRefNo() {
		return transactionRefNo;
	}

	public void setTransactionRefNo(String transactionRefNo) {
		this.transactionRefNo = transactionRefNo;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public Salesman getSalesman() {
		return salesman;
	}

	public void setSalesman(Salesman salesman) {
		this.salesman = salesman;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public double getDebit() {
		return debit;
	}

	public void setDebit(double debit) {
		this.debit = debit;
	}

	public double getCredit() {
		return credit;
	}

	public void setCredit(double credit) {
		this.credit = credit;
	}

	public double getBalance() {
		return balance;
	}

	public void setBalance(double balance) {
		this.balance = balance;
	}

}